package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.keyword.CreateKeywordBO;
import com.cskaoyan.bean.keyword.CreateKeywordVO;
import com.cskaoyan.bean.keyword.KeywordListUnitVO;
import com.cskaoyan.bean.keyword.KeywordReq;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.util.CskaoyanPageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: whyccz-mall-project
 * @description:
 * @create: 2021-06-06 22:27
 **/
@Service
public class KeywordServiceImpl implements KeywordService {
	@Autowired
	KeywordMapper keywordMapper;
	@Override
	public BaseData<KeywordListUnitVO> selectKeywordList(KeywordReq keywordReq) {

		CskaoyanPageHelper.startPage(keywordReq);//调用分页插件


		//每个service方法创建一个example对象
		KeywordExample keywordExample = new KeywordExample();
		//排序信息
		keywordExample.setOrderByClause(keywordReq.getSort()+" "+keywordReq.getOrder());
		KeywordExample.Criteria criteria = keywordExample.createCriteria();
		if (keywordReq.getKeyword() !=null&& !"".equals(keywordReq.getKeyword())){
			criteria.andKeywordLike("%" + keywordReq.getUrl() + "%");
		}
		if (keywordReq.getKeyword() !=null&& !"".equals(keywordReq.getKeyword())){
			criteria.andKeywordLike("%" + keywordReq.getUrl() + "%");
		}

		criteria.andDeletedEqualTo(false);

		List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);

		//获得分页信息
		PageInfo<Keyword> pageInfo = new PageInfo<>(keywords);
		long total = pageInfo.getTotal();

		return BaseData.ok(keywords,total);

	}

	@Override
	public CreateKeywordVO createKeyword(CreateKeywordBO createKeywordBO) {
		Keyword keyword = new Keyword();
		keyword.setKeyword(createKeywordBO.getKeyword());
		keyword.setUrl(createKeywordBO.getUrl());
		keyword.setAddTime(new Timestamp(System.currentTimeMillis()));
		keyword.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		keyword.setIsDefault(createKeywordBO.getIsDefault());
		keyword.setIsHot(createKeywordBO.getIsHot());
		keyword.setDeleted(false);
		keywordMapper.insertSelective(keyword);
		Integer id = keyword.getId();

		CreateKeywordVO keywordVO = new CreateKeywordVO();
		keywordVO.setId(id);
		keywordVO.setKeyword(createKeywordBO.getKeyword());
		keywordVO.setUrl(createKeywordBO.getUrl());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(new Date());
		keyword.setIsDefault(createKeywordBO.getIsDefault());
		keyword.setIsHot(createKeywordBO.getIsHot());
		keywordVO.setAddTime(format);
		keywordVO.setUpdateTime(format);
		return keywordVO;
	}

	@Override
	public CreateKeywordVO updateIssue(CreateKeywordVO keywordVO) {

		Keyword keyword = new Keyword();
		keyword.setId(keywordVO.getId());
		keyword.setKeyword(keywordVO.getKeyword());
		keyword.setUrl(keywordVO.getUrl());
		keyword.setIsDefault(keywordVO.getIsDefault());//如果是boolean对象会出错,返回的是default
		keyword.setIsHot(keywordVO.getIsHot());
		keyword.setAddTime(Timestamp.valueOf(keywordVO.getAddTime()));
		keyword.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		keyword.setDeleted(false);
		keywordMapper.updateByPrimaryKeySelective(keyword);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format = simpleDateFormat.format(new Date());
		keywordVO.setUpdateTime(format);
		return keywordVO;
	}

	@Override
	public void deleteIssue(CreateKeywordVO keywordDeleteVO) {
		keywordMapper.deleteById(keywordDeleteVO.getId());
	}


}
